package cn.iocoder.yudao.module.handguard.service.applicationdeductrecord;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.handguard.controller.admin.statistics.vo.PlatformProfitStatisticsResVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.math.BigDecimal;
import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.app.applicationdeductrecord.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.applicationdeductrecord.ApplicationDeductRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.applicationdeductrecord.ApplicationDeductRecordConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.applicationdeductrecord.ApplicationDeductRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 产品订单-提成明细记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ApplicationDeductRecordServiceImpl implements ApplicationDeductRecordService {

    @Resource
    private ApplicationDeductRecordMapper applicationDeductRecordMapper;

    @Override
    public Long createApplicationDeductRecord(ApplicationDeductRecordCreateReqVO createReqVO) {
        // 插入
        ApplicationDeductRecordDO applicationDeductRecord = ApplicationDeductRecordConvert.INSTANCE.convert(createReqVO);
        applicationDeductRecordMapper.insert(applicationDeductRecord);
        // 返回
        return applicationDeductRecord.getId();
    }

    @Override
    public void updateApplicationDeductRecord(ApplicationDeductRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateApplicationDeductRecordExists(updateReqVO.getId());
        // 更新
        ApplicationDeductRecordDO updateObj = ApplicationDeductRecordConvert.INSTANCE.convert(updateReqVO);
        applicationDeductRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteApplicationDeductRecord(Long id) {
        // 校验存在
        validateApplicationDeductRecordExists(id);
        // 删除
        applicationDeductRecordMapper.deleteById(id);
    }

    private void validateApplicationDeductRecordExists(Long id) {
        if (applicationDeductRecordMapper.selectById(id) == null) {
            throw exception(APPLICATION_DEDUCT_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ApplicationDeductRecordDO getApplicationDeductRecord(Long id) {
        return applicationDeductRecordMapper.selectById(id);
    }

    @Override
    public List<ApplicationDeductRecordDO> getApplicationDeductRecordList(Collection<Long> ids) {
        return applicationDeductRecordMapper.selectBatchIds(ids);
    }

    @Override
    public ApplicationDeductRecordDO getApplicationDeductRecordListByOrderId(Long orderId) {
        LambdaQueryWrapperX<ApplicationDeductRecordDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(ApplicationDeductRecordDO::getOrderId, orderId);
        ApplicationDeductRecordDO recordDOList = applicationDeductRecordMapper.selectOne(queryWrapper);
        return recordDOList;
    }

    @Override
    public PageResult<ApplicationDeductRecordDO> getApplicationDeductRecordPage(ApplicationDeductRecordPageReqVO pageReqVO) {
        return applicationDeductRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ApplicationDeductRecordDO> getApplicationDeductRecordList(ApplicationDeductRecordExportReqVO exportReqVO) {
        return applicationDeductRecordMapper.selectList(exportReqVO);
    }

    @Override
    public List<PlatformProfitStatisticsResVO>  getPlatformProfit(Integer staType) {
        return statisticsPack(staType);
    }


    public List<PlatformProfitStatisticsResVO> statisticsPack(Integer staType){
        List<PlatformProfitStatisticsResVO> res = new ArrayList<>();
        String nameStr = "日,周,月,季度,近半年,近一年";
        String [] nameArr = nameStr.split(",");
        if (staType==0){
            for (int i=0;i<nameArr.length;i++){
                BigDecimal value = applicationDeductRecordMapper.getPlatformProfit(i+1);
                if (value == null){
                    value  = BigDecimal.ZERO;
                }
                PlatformProfitStatisticsResVO resVO = new PlatformProfitStatisticsResVO();
                resVO.setName(nameArr[i]);
                resVO.setValue(value);
                res.add(resVO);
            }
        }else {
            BigDecimal value = applicationDeductRecordMapper.getPlatformProfit(staType);
            if (value == null){
                value  = BigDecimal.ZERO;
            }
            PlatformProfitStatisticsResVO resVO = new PlatformProfitStatisticsResVO();
            resVO.setName(nameArr[staType-1]);
            resVO.setValue(value);
            res.add(resVO);
        }
        return res;
    }



}
